<template>
  <div :id="chartId" :style="{'height':height}" class="echarts-box" />
</template>

<script>
// vue
import {
  defineComponent,
  toRefs,
  onMounted
} from 'vue'
  // echarts
import * as echarts from 'echarts'
// options
import {
  Line1Data
} from './Line1'

export default defineComponent({
  props: {
    chartId: {
      type: String,
      default: 'myId'
    },
    chartData: {
      type: Object,
      default: function() {
        return {}
      }
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  setup(props) {
    const {
      chartId,
      chartData,
      height
    } = toRefs(props)

    onMounted(async() => {
      // 初始化echarts实例
      const myChart = echarts.init(document.getElementById(chartId.value))
      // 绘制图表
      myChart.setOption(Line1Data)

      myChart.setOption({
        xAxis: {
          data: chartData.value.xdata
        },
        series: [{
          data: chartData.value.ydata
        }]
      })

      window.addEventListener('resize', () => {
        myChart.resize()
      })
    })
    return {
      chartId,
      chartData,
      height
    }
  }
})
</script>

<style lang="scss" scoped>
  .echarts-box {
    width: 100%;
  }
</style>
